#!/bin/sh
# Build a textsym file for use in the Arium ITP probe.

help() {
cat <<END
Build a WinDD "symtxt" file for use with the Arium ECM-30 probe.

	Usage: $0 [<vmlinux file> [<output file>]]
		If no input file is specified, it defaults to vmlinux.
		If no output file name is specified, it defaults to "textsym".
END
exit 1
}

err () {
	echo "ERROR - $*" >&2
	exit 1
}


OPTS="H"
while getopts "$OPTS" c ; do
	case $c in
		H)  help;;
		\?) help;;
	esac

done
shift `expr $OPTIND - 1`

LINUX=${1:-vmlinux}
TEXTSYM=${2:-${LINUX}.sym}

[ -f $VMLINUX ] || help


# pipe everything thru sort
echo "TEXTSYM V1.0"
(cat <<END 
GLOBAL | e000000000500000 | CODE | VEC_VHPT_Translation_0000
GLOBAL | e000000000500400 | CODE | VEC_ITLB_0400
GLOBAL | e000000000500800 | CODE | VEC_DTLB_0800
GLOBAL | e000000000500c00 | CODE | VEC_Alt_ITLB_0c00
GLOBAL | e000000000501000 | CODE | VEC_Alt_DTLB_1000
GLOBAL | e000000000501400 | CODE | VEC_Data_nested_TLB_1400
GLOBAL | e000000000501800 | CODE | VEC_Instruction_Key_Miss_1800
GLOBAL | e000000000501c00 | CODE | VEC_Data_Key_Miss_1c00
GLOBAL | e000000000502000 | CODE | VEC_Dirty-bit_2000
GLOBAL | e000000000502400 | CODE | VEC_Instruction_Access-bit_2400
GLOBAL | e000000000502800 | CODE | VEC_Data_Access-bit_2800
GLOBAL | e000000000502c00 | CODE | VEC_Break_instruction_2c00
GLOBAL | e000000000503000 | CODE | VEC_External_Interrupt_3000
GLOBAL | e000000000503400 | CODE | VEC_Reserved_3400
GLOBAL | e000000000503800 | CODE | VEC_Reserved_3800
GLOBAL | e000000000503c00 | CODE | VEC_Reserved_3c00
GLOBAL | e000000000504000 | CODE | VEC_Reserved_4000
GLOBAL | e000000000504400 | CODE | VEC_Reserved_4400
GLOBAL | e000000000504800 | CODE | VEC_Reserved_4800
GLOBAL | e000000000504c00 | CODE | VEC_Reserved_4c00
GLOBAL | e000000000505000 | CODE | VEC_Page_Not_Present_5000
GLOBAL | e000000000505100 | CODE | VEC_Key_Permission_5100
GLOBAL | e000000000505200 | CODE | VEC_Instruction_Access_Rights_5200
GLOBAL | e000000000505300 | CODE | VEC_Data_Access_Rights_5300
GLOBAL | e000000000505400 | CODE | VEC_General_Exception_5400
GLOBAL | e000000000505500 | CODE | VEC_Disabled_FP-Register_5500
GLOBAL | e000000000505600 | CODE | VEC_Nat_Consumption_5600
GLOBAL | e000000000505700 | CODE | VEC_Speculation_5700
GLOBAL | e000000000505800 | CODE | VEC_Reserved_5800
GLOBAL | e000000000505900 | CODE | VEC_Debug_5900
GLOBAL | e000000000505a00 | CODE | VEC_Unaligned_Reference_5a00
GLOBAL | e000000000505b00 | CODE | VEC_Unsupported_Data_Reference_5b00
GLOBAL | e000000000505c00 | CODE | VEC_Floating-Point_Fault_5c00
GLOBAL | e000000000505d00 | CODE | VEC_Floating_Point_Trap_5d00
GLOBAL | e000000000505e00 | CODE | VEC_Lower_Privilege_Tranfer_Trap_5e00
GLOBAL | e000000000505f00 | CODE | VEC_Taken_Branch_Trap_5f00
GLOBAL | e000000000506000 | CODE | VEC_Single_Step_Trap_6000
GLOBAL | e000000000506100 | CODE | VEC_Reserved_6100
GLOBAL | e000000000506200 | CODE | VEC_Reserved_6200
GLOBAL | e000000000506300 | CODE | VEC_Reserved_6300
GLOBAL | e000000000506400 | CODE | VEC_Reserved_6400
GLOBAL | e000000000506500 | CODE | VEC_Reserved_6500
GLOBAL | e000000000506600 | CODE | VEC_Reserved_6600
GLOBAL | e000000000506700 | CODE | VEC_Reserved_6700
GLOBAL | e000000000506800 | CODE | VEC_Reserved_6800
GLOBAL | e000000000506900 | CODE | VEC_IA-32_Exeception_6900
GLOBAL | e000000000506a00 | CODE | VEC_IA-32_Intercept_6a00
GLOBAL | e000000000506b00 | CODE | VEC_IA-32_Interrupt_6b00
GLOBAL | e000000000506c00 | CODE | VEC_Reserved_6c00
GLOBAL | e000000000506d00 | CODE | VEC_Reserved_6d00
GLOBAL | e000000000506e00 | CODE | VEC_Reserved_6e00
GLOBAL | e000000000506f00 | CODE | VEC_Reserved_6f00
GLOBAL | e000000000507000 | CODE | VEC_Reserved_7000
GLOBAL | e000000000507100 | CODE | VEC_Reserved_7100
GLOBAL | e000000000507200 | CODE | VEC_Reserved_7200
GLOBAL | e000000000507300 | CODE | VEC_Reserved_7300
GLOBAL | e000000000507400 | CODE | VEC_Reserved_7400
GLOBAL | e000000000507500 | CODE | VEC_Reserved_7500
GLOBAL | e000000000507600 | CODE | VEC_Reserved_7600
GLOBAL | e000000000507700 | CODE | VEC_Reserved_7700
GLOBAL | e000000000507800 | CODE | VEC_Reserved_7800
GLOBAL | e000000000507900 | CODE | VEC_Reserved_7900
GLOBAL | e000000000507a00 | CODE | VEC_Reserved_7a00
GLOBAL | e000000000507b00 | CODE | VEC_Reserved_7b00
GLOBAL | e000000000507c00 | CODE | VEC_Reserved_7c00
GLOBAL | e000000000507d00 | CODE | VEC_Reserved_7d00
GLOBAL | e000000000507e00 | CODE | VEC_Reserved_7e00
GLOBAL | e000000000507f00 | CODE | VEC_Reserved_7f00
END

$OBJDUMP -t $LINUX | sort | awk '
/empty_zero_page/ {start=1}
/e0000000/ {
	if ($4 == ".kdb")
		next
	if (start && substr($NF,1,1) != "0") {
		type = substr($0,26,5)
		if (type == ".text")
			printf "GLOBAL | %s | CODE | %s\n", $1, $NF
		else
			printf "GLOBAL | %s | DATA | %s | %d\n", $1, $NF, $(NF-1)
	}
	if($NF == "_end") 
		exit

}
' ) | egrep -v " __device| __vendor" | awk '
/GLOBAL/ {
	print $0
	print substr($0,1,9) substr($0,18,18) "Phy_" substr($0,36)

} ' | sort -k3



N=`wc -l $TEXTSYM|awk '{print $1}'`
echo "Generated TEXTSYM file" >&2
echo "  $LINUX --> $TEXTSYM" >&2
echo "  Found $N symbols" >&2
